package im.vinci.server.syncdb.persistence;

import im.vinci.server.syncdb.domain.MusicFeedRelation;
import im.vinci.server.syncdb.domain.UserFavoriteMusic;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;

/**
 * Created by mingjie on 16/12/21.
 */
@Repository
public interface MusicFeedRelationMapper {

    @Insert({"<script>",
            "INSERT IGNORE INTO music_feed_relation",
            "(user_id, song_id, song_type, feed_id) VALUES ",
            "<foreach item='item' index='index' collection='list' separator=','>",
            "(#{item.userId}, #{item.songId}, #{item.songType}, #{item.feedId})",
            "</foreach>",
            "</script>"
    })
    @Options(useGeneratedKeys = true, keyColumn = "id")
    public int insert(@Param("list") Collection<MusicFeedRelation> list);

    @Select({"<script>",
            "SELECT * FROM music_feed_relation WHERE user_id=#{user_id} AND song_id IN ",
            "<foreach item='item' index='index' collection='list' open='(' separator=',' close=')'>",
            "#{item.songId}",
            "</foreach>",
            "</script>"
    })
    public List<MusicFeedRelation> getData(@Param("user_id") long userId, @Param("list") Collection<UserFavoriteMusic> songIdList);

    @Delete("DELETE FROM music_feed_relation WHERE user_id=#{item.userId} and song_id=#{item.songId} and song_type=#{item.songType}")
    public int delete(@Param("item") UserFavoriteMusic userFavoriteMusic);
}
